<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head>
	<th:block th:include="include :: bootstrap-editable-css" />
	<th:block th:include="include :: header('新建入库单')"/>
	<th:block th:include="include :: layout-latest-css"/>
	<th:block th:include="include :: ztree-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
	<div class="col-sm-12 search-collapse">
		<form id="in-form">
			<div class="select-list">
				<ul>
					<li>
						<label class="is-required">入库类型：</label>
						<select name="inType" required>
<!--							<option value="">请选择</option>-->
							<option th:each="item:${basTypeList}"
									th:value="${item.typeValue}" th:text="${item.typeName}">
							</option>
						</select>
					</li>
					<li>
						<label>建单时间：</label><input type="text" id="createDate" name="createDate"/>
					</li>
					<li><label>单据号：</label><input type="text" id="stockinId" placeholder="自动生成" readonly></li>
<!--					<li>-->
<!--						<lable>供应商：</lable>-->
<!--						<input id="supplierId" name="supplierId" hidden>-->
<!--						<input id="supplierName" name="supplierName" placeholder="请点击右侧按钮选择" readonly required>-->
<!--						<a id="selectSupplier" class="btn btn-success" onclick="selectSupplier()" >-->
<!--							...-->
<!--						</a>-->
<!--					</li>-->
					<li>
						<a id="save" class="btn btn-primary btn-rounded btn-sm" onclick="saveOutStock()"><i
								class="fa fa-search"></i>&nbsp;保存单据</a>
						<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
								class="fa fa-refresh"></i>&nbsp;重置</a>
					</li>
				</ul>
			</div>
		</form>
	</div>
	<div class="btn-group-sm" id="toolbar" role="group">
		<a id="selectProduct" class="btn btn-success" onclick="selectProduct()" >
			<i class="fa fa-plus"></i> 选择物资
		</a>
		<a id="continue" class="btn btn-success" onclick="newReceiptAgain()" >
			<i class="fa fa-plus"></i> 继续建单
		</a>
		<a id="delete" class="btn btn-danger multiple disabled" onclick="deleteProduct()" >
			<i class="fa fa-remove"></i> 删除物资
		</a>
	</div>

	<div id="div" class="col-sm-12 select-table table-striped">
		<table id="bootstrap-table" data-mobile-responsive="true"></table>
	</div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-editable-js" />
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<script th:inline="javascript">
	var editFlag = [[${@permission.hasPermi('mydo:stockin:edit')}]];
	var removeFlag = [[${@permission.hasPermi('mydo:stockin:remove')}]];
	var prefix = ctx + "mydo/stockin"

	// layui.use('laydate', function(){
	// 	var laydates = layui.laydate;
	// 	laydates.render({
	// 		elem: '#createDate'
	// 		,type: 'datetime',
	// 		/* ,range: '到' */
	// 		format: 'yyyy-MM-dd HH:mm:ss'
	// 	})
	// })
	$(function () {
		var options = {
			// url: "/out/stockoutview/list",
			createUrl: prefix + "/addSave",
			clickToSelect: true,
			contenteditable:true,
			height: $("#div").height()/1,
			// updateUrl: prefix + "/edit/{id}",
			// removeUrl: prefix + "/remove",
			// exportUrl: prefix + "/export",
			// saveUrl:"/out/stockout/addSave",
			modalName: "出库主单",
			editable:true,
			columns: [{
				checkbox: true,

			},

				{
					field: 'productGuid',
					title: 'ID',
					visible: false,
				},
				{
					field: 'productId',
					title: '物资编码'
				},
				{
					field: 'productName',
					title: '物资名称'
				},
				// {
				// 	field: 'stockinId',
				// 	title: '单据号'
				//
				// },

				// {
				// 	field: 'price',
				// 	title: '单价'
				//
				// },
				// {
				// 	field: 'assignNum',
				// 	title: '实际数量'
				//
				// },
				// {
				// 	field: 'stockStaus',
				// 	title: '库存状态'
				//
				// },

				{
					field: 'quantity',
					title: '数量',
					visible: true,
					// editable: {
					// 	type: 'text',
					// 	title: '数量',
					// 	disabled : false, // 是否禁用编辑
					// 	validate: function (value) {
					// 		if (isNaN(value)) {
					// 			return '请填写数字';
					// 		}
					// 		if (value.length == 0) {
					// 			return '数量不能为空';
					// 		}
					// 	}
					//
					// },
					formatter: function (value, row, index) {
						if (value) {
							return value;
						} else {
							row.quantity = '';
							return ;
						}
					}
				},
				{
					field: 'lotNo',
					title: '批号',
					visible: true,
					// editable: {
					// 	type: 'text',
					// 	title: '批号',
					// 	disabled : false, // 是否禁用编辑
					// 	validate: function (value) {
					// 		// if (isNaN(value)) {
					// 		// 	return '请填写数字';
					// 		// }
					// 	}
					//
					// },
					formatter: function (value, row, index) {
						if (value) {
							return value;
						} else {
							row.lotNo = '';
							return ;
						}
					}
				},
				// {
				// 	field: 'stockStatus',
				// 	title: '单位',
				// 	visible: true,
				// 	// editable: {
				// 	// 	type: 'text',
				// 	// 	title: '单位',
				// 	// 	disabled : false, // 是否禁用编辑
				// 	// 	validate: function (value) {
				// 	// 		// if (isNaN(value)) {
				// 	// 		// 	return '请填写单位';
				// 	// 		// }
				// 	// 	}
				// 	//
				// 	// },
				// 	formatter: function (value, row, index) {
				// 		if (value) {
				// 			return value;
				// 		} else {
				// 			row.stockStatus = '';
				// 			return ;
				// 		}
				// 	}
				//
				// },
				{
					field: 'remark',
					title: '备注',
					visible: true,
					// editable: {
					// 	type: 'text',
					// 	title: '备注',
					// 	disabled : false, // 是否禁用编辑
					// 	validate: function (value) {
					// 		// if (isNaN(value)) {
					// 		// 	return '请填写备注';
					// 		// }
					// 	}
					//
					// },
					formatter: function (value, row, index) {
						if (value) {
							return value;
						} else {
							row.remark = '';
							return ;
						}
					}

				},

			],
			onClickCell: function (field, value, row, $element) {
				if (field == "remark" || field == "stockStatus" || field == "lotNo" || field == "quantity") {
					$element.attr('contenteditable', true);
					$element.blur(function () {
						let tdValue = $element.html();
						tdValue = tdValue.replace(/<br>/g, "")

						switch (field) {
							case "remark":
								row.remark = tdValue;
								break;
							case "stockStatus":
								row.stockStatus = tdValue;
								break;
							case "lotNo":
								row.lotNo = tdValue;
								break;
							case "quantity":
								row.quantity = tdValue;
								break;
						}
					})
				}
			}
		};
		$.table.init(options);
		createTime();
	});
	//日期框
	function createTime(){
		var _input = document.getElementById('createDate');

		var date = new Date();
		var seperator = "";
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentDate = year + seperator + month + seperator + strDate; //当前日期

		_input.value = currentDate; //赋值给input.value
	}
	//选择物资
	function selectProduct() {
		var url = "/mydo/stockin/selectPro";
		$.modal.open("选择物品信息", url);

	}
	//关闭物资框
	function closeSelectProduct(row) {

		if (row) {

			var rows2= $('#bootstrap-table').bootstrapTable('getData');
			for(var i=0;i<rows2.length;i++){
				for(var j=0;j<row.length;j++){
					if(rows2[i].productGuid==row[j].productGuid){
						$.modal.alertWarning("已存在该物资")
						$.modal.close();
						return;
					}

				}

			}

			var zTreeDoorData = [];
			// var doorIdArr = data.doorIdArr;
			//点击每个节点重新获取数据的初始化操作
			// $('#bootstrap-table').bootstrapTable('removeAll');
			zTreeDoorData.splice(0, zTreeDoorData.length);
			for (var i = 0; i < row.length; i++) {

				treedoor = {
					productGuid: row[i].productGuid,
					productId: row[i].productId,
					productName: row[i].productName,
					// stockQuantity:row[i].quantity,
					// onlyIdentity:row[i].onlyIdentity
					// lotNo:row[i].lotNo,
					// stockStatus:row[i].stockStatus,
					// printDate:row[i].printDate


				}
				zTreeDoorData.push(treedoor);
			} //forEnd

			for (var j = 0; j < zTreeDoorData.length; j++) {
				var dataTree2 = zTreeDoorData[j];
				$('#bootstrap-table').bootstrapTable('append', dataTree2);

			}
			console.log(row);
		}

	}

	//保存单据
	function saveOutStock() {

		var rows= $('#bootstrap-table').bootstrapTable('getData');
		if(rows.length==0){
			$.modal.alertWarning("请选择物资");
			return;
		}
		for(var i=0;i<rows.length;i++) {
			if(rows[i].quantity==""){
				$.modal.alertWarning("请填写入库数量")
				return;
			}
			if(rows[i].lotNo==""){
				$.modal.alertWarning("请填写批号")
				return;
			}
			if(rows[i].stockStatus==""){
				$.modal.alertWarning("请填写单位")
				return;
			}
		}

		if($.validate.form()){
			var params=new Object();
			params.stockin=$("#in-form").serializeJson();
			params.stockinDetailList=$("#bootstrap-table").bootstrapTable("getData",false);
			var dada=JSON.stringify(params);
			console.log(dada)

			var opt=new Object()

			opt.url = '/mydo/stockin/addSave';
			opt.data=JSON.stringify(params);

			// window.alert(opt.data);
			ajaxRequestJsonBody(opt,onSaveSuccess);
			function ajaxRequestJsonBody(opt,callback){

				$.ajax({
					url			: opt.url,
					/**必须是POST方法**/
					type		: 'post',
					/**必须制定请求的类型**/
					contentType :'application/json',
					data		: opt.data,
					dataType	: 'json',
					success : function(result) {
							$.modal.alert(result.msg);

						if(callback)
							callback(result);
					},
					error:function(XMLHttpRequest,textStatus,error){
						DataTable.config.ajaxError(XMLHttpRequest,textStatus,error,"设置失败");
					}
				});
			}

		}
		modal={
			// 弹出提示
			alert: function(content, type) {
				$.messager.show({
				    icon: type,
				    title: "系统提示",
				    msg: content,
				    timeout:2000
				});
			},
			// 错误提示
			alertError: function(content) {
				modal.alert(content, modal_status.FAIL);
			},
			// 成功提示
			alertSuccess: function(content) {
				modal.alert(content, modal_status.SUCCESS);
			},
			// 警告提示
			alertWarning: function(content) {
				modal.alert(content, modal_status.WARNING);
			},
			// 重新加载
			reload: function () {
				parent.location.reload();
			}
		}
	}
	//保存单据后的回调函数
	function onSaveSuccess(data) {
		if(data.code == web_status.SUCCESS){
			$('#stockinId').attr("value",data.data);
			$('#save').addClass('disabled'); // Disables visually
			$('#selectProduct').addClass('disabled'); // Disables visually
			$('#delete').addClass('disabled'); // Disables visually
			$('#reset').addClass('disabled');

		}
	}
	//继续建单
	function newReceiptAgain(){
		window.location.reload();
		$.form.reset()
	}

	//删除物资
	function deleteProduct() {



		//获取选中行的内容
		var contents = $.map($("#bootstrap-table").bootstrapTable('getSelections'), function (row) {
			return row.productGuid;
		});

		// var rows=$("#bootstrap-table").bootstrapTable('getSelections');
		//
		// var content=rows.checked;
		// console.log("3333333",content);

		//前端删除数据
		$("#bootstrap-table").bootstrapTable('remove', {
			field: 'productGuid',
			values: contents
		});
	}

	// 选择供应商
	function selectSupplier(){
		var url = "/mydo/stockin/selectSupplier";
		$.modal.open("选择物品信息", url);
	}

	// 关闭供应商框
	function closeSelectSupplier(row){
		$("#supplierId").val(row[0].supplierNo);
		$("#supplierName").val(row[0].supplierName);
	}
</script>
</body>
</html>